package com.nanyang.tms.controller.shipment;

import java.util.List;

import javax.validation.Valid;

import com.nanyang.tms.api.dto.request.shipment.ShipmentBillFuzzyQueryRequest;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillFuzzyPageResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillAddRequest;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillQueryRequest;
import com.nanyang.tms.api.dto.request.shipment.ShipmentBillUpdateStateRequest;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillDetailResponse;
import com.nanyang.tms.api.dto.response.shipment.ShipmentBillPageResponse;
import com.nanyang.tms.api.service.shipment.ShipmentBillRpcService;

@RestController
@RequestMapping("/shipment")
public class ShipmentBillController {
    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private ShipmentBillRpcService shipmentRpcService;
    /**
     * <p>listShipmentBill</p>
     * <p>Description:查询出货单列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<ShipmentBillPageResponse>> listShipmentBill(@RequestBody ShipmentBillQueryRequest param) {
        ApiResult<PageBean<ShipmentBillPageResponse>> apiResult = shipmentRpcService.listShipmentBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listShipmentBill</p>
     * <p>Description:模糊查询出货单列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/listFuzzy", method = RequestMethod.POST)
    public ApiResult<PageBean<ShipmentBillFuzzyPageResponse>> listShipmentBill(@RequestBody ShipmentBillFuzzyQueryRequest param) {
        ApiResult<PageBean<ShipmentBillFuzzyPageResponse>> apiResult = shipmentRpcService.listFuzzyShipmentBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listShipmentBill</p>
     * <p>Description:查询根据编码</p>
     *
     * @param shipmentCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{shipmentCode}", method = RequestMethod.GET)
    public ApiResult<List<ShipmentBillDetailResponse>> selectByCode(@PathVariable("shipmentCode") String shipmentCode) {
        ApiResult<List<ShipmentBillDetailResponse>> apiResult = shipmentRpcService.selectByCode(shipmentCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> saveShipmentBill(@BindParam @RequestBody @Valid ShipmentBillAddRequest param) {
        ApiResult<Boolean> apiResult = shipmentRpcService.saveShipmentBill(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 设置完成
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public ApiResult<Boolean> updateState(@BindParam @RequestBody @Valid ShipmentBillUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = shipmentRpcService.updateState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 作废
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/cancelState", method = RequestMethod.POST)
    public ApiResult<Boolean> cancelState(@BindParam @RequestBody @Valid ShipmentBillUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = shipmentRpcService.cancelState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
